﻿
namespace SPL.Windows.Forms
{
	/// <summary>
	/// abstract Batch typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class BatchDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<BatchDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.Batch _currentBatch = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxBatchDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxBatchErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxBatchBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the Id property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the BatchNo property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxBatchNoDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the Type property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTypeDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the CreatedBy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxCreatedByDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the CreatedAt property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxCreatedAtDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.User> _CreatedByList;
		
		/// <summary> 
		/// The list of selectable User
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.User> CreatedByList
		{
			get {return this._CreatedByList;}
			set 
			{
				this._CreatedByList = value;
				this.uxCreatedByDataGridViewColumn.DataSource = null;
				this.uxCreatedByDataGridViewColumn.DataSource = this._CreatedByList;
			}
		}
		
		private bool _allowNewItemInCreatedByList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of User
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInCreatedByList
		{
			get { return _allowNewItemInCreatedByList;}
			set
			{
				this._allowNewItemInCreatedByList = value;
				this.uxCreatedByDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.Batch> _BatchList;
				
		/// <summary> 
		/// The list of Batch to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.Batch> BatchList
		{
			get {return this._BatchList;}
			set
			{
				this._BatchList = value;
				this.uxBatchBindingSource.DataSource = null;
				this.uxBatchBindingSource.DataSource = value;
				this.uxBatchDataGridView.DataSource = null;
				this.uxBatchDataGridView.DataSource = this.uxBatchBindingSource;				
				//this.uxBatchBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxBatchBindingSource_ListChanged);
				this.uxBatchBindingSource.CurrentItemChanged += new System.EventHandler(OnBatchBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnBatchBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentBatch = uxBatchBindingSource.Current as Entities.Batch;
			
			if (_currentBatch != null)
			{
				_currentBatch.Validate();
			}
			//_Batch.Validate();
			OnCurrentEntityChanged();
		}

		//void uxBatchBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <c cref="Entities.Batch"/> instance.
		/// </summary>
		public Entities.Batch SelectedBatch
		{
			get {return this._currentBatch;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxBatchDataGridView.VirtualMode;}
			set
			{
				this.uxBatchDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxBatchDataGridView.AllowUserToAddRows;}
			set {this.uxBatchDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxBatchDataGridView.AllowUserToDeleteRows;}
			set {this.uxBatchDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <c cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxBatchDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxBatchDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="BatchDataGridViewBase"/> class.
		/// </summary>
		public BatchDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxBatchDataGridView = new System.Windows.Forms.DataGridView();
			this.uxBatchBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxBatchErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxBatchNoDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTypeDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxCreatedByDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxCreatedAtDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			//this.uxCreatedByBindingSource = new UserBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxCreatedByBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxBatchErrorProvider
			// 
			this.uxBatchErrorProvider.ContainerControl = this;
			this.uxBatchErrorProvider.DataSource = this.uxBatchBindingSource;						
			// 
			// uxBatchDataGridView
			// 
			this.uxBatchDataGridView.AutoGenerateColumns = false;
			this.uxBatchDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxBatchDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxIdDataGridViewColumn,
		this.uxBatchNoDataGridViewColumn,
		this.uxTypeDataGridViewColumn,
		this.uxCreatedByDataGridViewColumn,
		this.uxCreatedAtDataGridViewColumn			});
			this.uxBatchDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxBatchDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxBatchDataGridView.Name = "uxBatchDataGridView";
			this.uxBatchDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxBatchDataGridView.TabIndex = 0;	
			this.uxBatchDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxBatchDataGridView.EnableHeadersVisualStyles = false;
			this.uxBatchDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnBatchDataGridViewDataError);
			this.uxBatchDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnBatchDataGridViewCellValueNeeded);
			this.uxBatchDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnBatchDataGridViewCellValuePushed);
			
			//
			// uxIdDataGridViewColumn
			//
			this.uxIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIdDataGridViewColumn.DataPropertyName = "Id";
			this.uxIdDataGridViewColumn.HeaderText = "Id";
			this.uxIdDataGridViewColumn.Name = "uxIdDataGridViewColumn";
			this.uxIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxBatchNoDataGridViewColumn
			//
			this.uxBatchNoDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxBatchNoDataGridViewColumn.DataPropertyName = "BatchNo";
			this.uxBatchNoDataGridViewColumn.HeaderText = "BatchNo";
			this.uxBatchNoDataGridViewColumn.Name = "uxBatchNoDataGridViewColumn";
			this.uxBatchNoDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxBatchNoDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxBatchNoDataGridViewColumn.ReadOnly = false;		
			//
			// uxTypeDataGridViewColumn
			//
			this.uxTypeDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTypeDataGridViewColumn.DataPropertyName = "Type";
			this.uxTypeDataGridViewColumn.HeaderText = "Type";
			this.uxTypeDataGridViewColumn.Name = "uxTypeDataGridViewColumn";
			this.uxTypeDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTypeDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTypeDataGridViewColumn.ReadOnly = false;		
			//
			// uxCreatedByDataGridViewColumn
			//
			this.uxCreatedByDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxCreatedByDataGridViewColumn.DataPropertyName = "CreatedBy";
			this.uxCreatedByDataGridViewColumn.HeaderText = "CreatedBy";
			this.uxCreatedByDataGridViewColumn.Name = "uxCreatedByDataGridViewColumn";
			this.uxCreatedByDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxCreatedByDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxCreatedByDataGridViewColumn.ReadOnly = false;		
			//
			// uxCreatedAtDataGridViewColumn
			//
			this.uxCreatedAtDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxCreatedAtDataGridViewColumn.DataPropertyName = "CreatedAt";
			this.uxCreatedAtDataGridViewColumn.HeaderText = "CreatedAt";
			this.uxCreatedAtDataGridViewColumn.Name = "uxCreatedAtDataGridViewColumn";
			this.uxCreatedAtDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxCreatedAtDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxCreatedAtDataGridViewColumn.ReadOnly = false;		
			//
			// uxCreatedByDataGridViewColumn
			//				
			this.uxCreatedByDataGridViewColumn.DisplayMember = "Username";	
			this.uxCreatedByDataGridViewColumn.ValueMember = "Id";	
			this.uxCreatedByDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxCreatedByDataGridViewColumn.DataSource = uxCreatedByBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxBatchDataGridView);
			this.Name = "BatchDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxCreatedByBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxBatchBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				BatchDataGridViewEventArgs args = new BatchDataGridViewEventArgs();
				args.Batch = _currentBatch;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class BatchDataGridViewEventArgs : System.EventArgs
		{
			private Entities.Batch	_Batch;
	
			/// <summary>
			/// the  Entities.Batch instance.
			/// </summary>
			public Entities.Batch Batch
			{
				get { return _Batch; }
				set { _Batch = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxBatchDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnBatchDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxBatchDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnBatchDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxBatchDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						e.Value = BatchList[e.RowIndex].Id;
						break;
					case "uxBatchNoDataGridViewColumn":
						e.Value = BatchList[e.RowIndex].BatchNo;
						break;
					case "uxTypeDataGridViewColumn":
						e.Value = BatchList[e.RowIndex].Type;
						break;
					case "uxCreatedByDataGridViewColumn":
						e.Value = BatchList[e.RowIndex].CreatedBy;
						break;
					case "uxCreatedAtDataGridViewColumn":
						e.Value = BatchList[e.RowIndex].CreatedAt;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxBatchDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnBatchDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxBatchDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxIdDataGridViewColumn":
						BatchList[e.RowIndex].Id = (System.Int32)e.Value;
						break;
					case "uxBatchNoDataGridViewColumn":
						BatchList[e.RowIndex].BatchNo = (System.Int32)e.Value;
						break;
					case "uxTypeDataGridViewColumn":
						BatchList[e.RowIndex].Type = (System.Int32)e.Value;
						break;
					case "uxCreatedByDataGridViewColumn":
						BatchList[e.RowIndex].CreatedBy = (System.Int32)e.Value;
						break;
					case "uxCreatedAtDataGridViewColumn":
						BatchList[e.RowIndex].CreatedAt = (System.DateTime)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
